package com.example.demo.Controller;

import com.example.demo.Enity.Activity;
import com.example.demo.dao.ActivityJpa;
import com.example.demo.dao.IndexJpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class Publish {
    @Autowired
    ActivityJpa activityJpa;
    @Autowired
    IndexJpa indexJpa;
    private File executeUpload(String uploadDir, MultipartFile file) throws Exception {

        //获得文件后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        //上传文件名,文件名字随机产生
        String filename = UUID.randomUUID() + suffix;

        //服务器端保存的文件对象,此文件拥有全部地址
        File serverFile = new File(uploadDir + filename);

        //将上传的文件写入到服务器端文件内
        file.transferTo(serverFile.getAbsoluteFile());

        return serverFile;
    }
    @RequestMapping("/addNum")
    public void addNum(String category){
        indexJpa.addNum(category);
    }
    //上传活动详情数据

    @RequestMapping(value = "upAcitivity2")
    @ResponseBody
    public String upAcitivity2(String file,String address,String category,String ided,String information,String master,String time,String wx) {

        Activity activity=new Activity();
        //将数据添加在数据库中
        activity.setAddress(address);
        activity.setCategory(category);
        activity.setIded(ided);
        activity.setImg(file);
        activity.setInformation(information);
        activity.setMaster(master);
        activity.setTime(time);
        activity.setWx(wx);
        activityJpa.save(activity);
        //活动数量加一
        indexJpa.addNum(category);
        return file;
    }
    //上传活动详情数据
    @RequestMapping(value = "upAcitivity", method = RequestMethod.POST)
    @ResponseBody
    public String uppetinfo(String file,String address,String category,String ided,String information,String master,String time,String wx) {

                Activity activity=new Activity();
                //将数据添加在数据库中
                activity.setAddress(address);
                activity.setCategory(category);
                activity.setIded(ided);
                activity.setImg(file);
                activity.setInformation(information);
                activity.setMaster(master);
                activity.setTime(time);
                activity.setWx(wx);
                activityJpa.save(activity);
                //活动数量加一
                indexJpa.addNum(category);
                return file;
    }
    //上传图片到服务器
    @RequestMapping(value = "upImg", method = RequestMethod.POST)
    @ResponseBody
    public String upImg(MultipartFile file) {
        try {
            String path = "/www/wwwroot/120.78.199.38/picture/";//设置保存在服务器的绝对路径
            File dir = new File(path);
            if (!dir.exists()) {//若不存在文件夾，則新建
                dir.mkdir();
            }
            //调用上传方法
            if (file != null) {
                //将文件上传到服务器
                File serviceFile = executeUpload(path, file);
                ////返回文件路径
                String url = "http://120.78.199.38:80/picture/" + serviceFile.getName();
                return url;
            }
    }catch(Exception e){
        return e.getMessage();}
        return  "错误";
    }
    @RequestMapping(value = "post", method = RequestMethod.POST)
    public String post(String post){
        return post;
    }
}

